//
//  RecordViewController.m
//  Rail
//
//  Created by hychen on 2018/2/26.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "RecordViewController.h"
#import "RecordTableViewCell.h"

@interface RecordViewController ()

@end

@implementation RecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData {
    BMWeakSelf
    [HttpRequest loadHistoryWatchListDataWithType:@"0" success:^(NSArray *object) {
        weakSelf.dataArray = [NSMutableArray arrayWithArray:object?:@[]];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView reloadData];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (void)removePatrolItemWithIndexPath:(NSIndexPath *)indexPath {
    SelectVideoDeviceModel *model = [self.dataArray objectAtIndexSafe:indexPath.row];
    if (![model isKindOfClass:[SelectVideoDeviceModel class]]) {
        return;
    }
    Show_NoMsg
    Show_ActionDisabled
    BMWeakSelf
    [HttpRequest deleteHistoryWatchDataWithSeqId:model.seqid success:^(id object) {
        [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
        [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        Dismiss_NoMsg
        Show_ActionEnabled
    } failure:^(NSString *code, NSString *msg) {
        Show_ActionEnabled
        Show_Msg_Error(msg)
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SelectVideoDeviceModel *model = [self.dataArray objectAtIndex:indexPath.row];
    if ([model isKindOfClass:[NSObject class]]) {
        RecordTableViewCell *cell = [RecordTableViewCell cellWithTableView:tableView class:[RecordTableViewCell class]];
        cell.model = model;
        return cell;
    }
    
    return [UITableViewCell new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SelectVideoDeviceModel *model = [self.dataArray objectAtIndex:indexPath.row];
    [self gotoDeviceDetail:model];
//    [JumpTool gotoWatchVideoVc:self deviceModel:model];
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

//设置进入编辑状态时，Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

//点击删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //在这里实现删除操作
    [self removePatrolItemWithIndexPath:indexPath];
}

- (void)gotoDeviceDetail:(SelectVideoDeviceModel *)model {
    //    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    Show_Msg(@"请稍等...")
    BMWeakSelf
    [HttpRequest loadDeviceVideoDetailDataWithDeviceId:model.deviceid success:^(SelectVideoDeviceModel *object) {
        //        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
        Dismiss_NoMsg
        if ([object isKindOfClass:[SelectVideoDeviceModel class]]) {
            [JumpTool gotoWatchVideoVc:weakSelf deviceModel:object];
        }
    } failure:^(NSString *code, NSString *msg) {
        //        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
        Show_SV_Error_Msg(msg)
    }];
}
@end
